//
//  MLWMyRecipeViewController.m
//  KITCHEN_STORIES
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWMyRecipeViewController.h"
#import "MLWCollectViewController.h"
#import "MLWLanguageTableViewCell.h"
#import "SDImageCache.h"
#import "MLWMapViewController.h"
#import "EATheme.h"  //夜间模式
@interface MLWMyRecipeViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 定义tableView */
@property (nonatomic, retain) UITableView *tableView;

/** 左侧图标 */
@property (nonatomic, retain) UIImageView *setCollectImageView;
@property (nonatomic, retain) UIImageView *setClearImageView;
@property (nonatomic, retain) UIImageView *setNightImageView;
@property (nonatomic, retain) UIImageView *setLanguageImageView;

/** 中间的label */
@property (nonatomic, retain) UILabel *collectLabel;
@property (nonatomic, retain) UILabel *clearLabel;
@property (nonatomic, retain) UILabel *nightLabel;
@property (nonatomic, retain) UILabel *languageLabel;

/** 右侧Button */
@property (nonatomic, retain) UIButton *collectButton;
@property (nonatomic, retain) UIButton *clearButton;
@property (nonatomic, retain) UIButton *languageButton;

/** 开关 */
@property (nonatomic, retain) UISwitch *s;

/** 需要清理的缓存值 */
@property (nonatomic, assign) CGFloat caCheNumber;

/** 定义语言种类的tableView */
@property (nonatomic, retain) UITableView *languageTableView;

//** 语言tableView 上的label */
@property (nonatomic, retain) UILabel *englishLabel;
@property (nonatomic, retain) UILabel *chineseLabel;

/** 语言tableView 上的button */
@property (nonatomic, retain) UIButton *langEngButton;
@property (nonatomic, retain) UIButton *langChiButton;
/** button 点击次数 */
@property (nonatomic, assign) NSInteger a;
@end

@implementation MLWMyRecipeViewController
- (void)dealloc
{
    [self.tableView release];
    [self.setCollectImageView release];
    [self.setClearImageView release];
    [self.setNightImageView release];
    [self.setLanguageImageView release];
    [self.collectLabel release];
    [self.clearLabel release];
    [self.nightLabel release];
    [self.languageLabel release];
    [self.s release];
    [self.languageTableView release];
    [self.englishLabel release];
    [self.chineseLabel release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的设置";
    
    //初始化:
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) style:UITableViewStylePlain];
    
    //设置代理人:
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //去除分割线:
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    //注册cell:
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //自定义导航栏右侧按钮:
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickedShareButton)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    //初始化cell上的基本控件:
    //收藏:
    self.setCollectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth / 10, ScreenHeight / 20, 32, 32)];
    self.setCollectImageView.image = [UIImage imageNamed:@"collect1"];
    [self.tableView addSubview:self.setCollectImageView];
    [self.setCollectImageView release];
    self.collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.setCollectImageView.frame.origin.x + 32 + 20, self.setCollectImageView.frame.origin.y, ScreenWidth / 3, 40)];
    self.collectLabel.text = @"我的收藏";
    self.collectLabel.font = [UIFont systemFontOfSize:16];
    self.collectLabel.textColor = [UIColor blueColor];
    [self.tableView addSubview:self.collectLabel];
    [self.collectLabel release];
    self.collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectButton.frame = CGRectMake(ScreenWidth - 70, self.collectLabel.frame.origin.y + 5, 32, 32);
    [self.collectButton setImage:[UIImage imageNamed:@"checkRed"] forState:UIControlStateNormal];
    [self.collectButton addTarget:self action:@selector(didClickedCollectButton) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView addSubview:self.collectButton];
    
    
    //清理缓存:
    self.setClearImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.setCollectImageView.frame.origin.x, self.setCollectImageView.frame.origin.y + ((ScreenHeight - 64 - 49) / 7), 32, 32)];
    self.setClearImageView.image = [UIImage imageNamed:@"clear1"];
    [self.tableView addSubview:self.setClearImageView];
    [self.setClearImageView release];
    self.clearLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.collectLabel.frame.origin.x, self.collectLabel.frame.origin.y + ((ScreenHeight - 64 - 49) / 7), ScreenWidth / 3, 40)];
    self.clearLabel.text = @"清理缓存";
    self.clearLabel.font = [UIFont systemFontOfSize:16];
    self.clearLabel.textColor = [UIColor blueColor];
    [self.tableView addSubview:self.clearLabel];
    [self.clearLabel release];
    self.clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.clearButton.frame = CGRectMake(ScreenWidth - 70, self.clearLabel.frame.origin.y + 5, 32, 32);
    [self.clearButton setImage:[UIImage imageNamed:@"checkRed"] forState:UIControlStateNormal];
    [self.clearButton addTarget:self action:@selector(didClickedClearButton) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView addSubview:self.clearButton];
    
    
    
    //夜间模式:
    self.setNightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.setClearImageView.frame.origin.x, self.setClearImageView.frame.origin.y + ((ScreenHeight - 64 - 49) / 7), 32, 32)];
    self.setNightImageView.image = [UIImage imageNamed:@"night1"];
    [self.tableView addSubview:self.setNightImageView];
    [self.setNightImageView release];
    self.nightLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.clearLabel.frame.origin.x, self.clearLabel.frame.origin.y + ((ScreenHeight - 64 - 49) / 7), ScreenWidth / 3, 40)];
    self.nightLabel.text = @"夜间模式";
    self.nightLabel.textColor = [UIColor blueColor];
    self.nightLabel.font = [UIFont systemFontOfSize:16];
    [self.tableView addSubview:self.nightLabel];
    [self.nightLabel release];
    
    //加入夜间模式开关:
    self.s = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth - 70, self.nightLabel.frame.origin.y + 5, 60, 50)];
    //外观颜色
    self.s.tintColor = [UIColor redColor];
    //打开是颜色:
    self.s.onTintColor = [UIColor greenColor];
    //设定开关状态:
    //[s setOn:YES];
    //添加动画效果:
    [self.s setOn:NO animated:YES];
    //开关的触发方法:
    [self.s addTarget:self action:@selector(didClickSwitch:) forControlEvents:UIControlEventValueChanged];
    
    
    [self.tableView addSubview:self.s];
    [self.s release];

    
    //语言:
    self.setLanguageImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.setNightImageView.frame.origin.x, self.setNightImageView.frame.origin.y + ((ScreenHeight - 64 - 49) / 7), 32, 32)];
    self.setLanguageImageView.image = [UIImage imageNamed:@"language1"];
    [self.tableView addSubview:self.setLanguageImageView];
    [self.setLanguageImageView release];
    self.languageLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nightLabel.frame.origin.x, self.nightLabel.frame.origin.y + ((ScreenHeight - 64 - 49) / 7), ScreenWidth / 3, 40)];
    self.languageLabel.text = @"语言种类";
    self.languageLabel.textColor = [UIColor blueColor];
    self.languageLabel.font = [UIFont systemFontOfSize:16];
    [self.tableView addSubview:self.languageLabel];
    [self.languageLabel release];
    
    self.languageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.languageButton.frame = CGRectMake(ScreenWidth - 70, self.languageLabel.frame.origin.y + 5, 32, 32);
    [self.languageButton setImage:[UIImage imageNamed:@"checkRed"] forState:UIControlStateNormal];
    [self.languageButton addTarget:self action:@selector(didClickedLanguageButton) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView addSubview:self.languageButton];
    
    //初始化语言种类的tableView
    
    self.languageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.languageLabel.frame.origin. y + self.languageLabel.frame.size.height + 10, ScreenWidth, ((ScreenHeight - 64 - 49) / 7) * 2) style:UITableViewStylePlain];
    //设置代理:
    self.languageTableView.delegate = self;
    self.languageTableView.dataSource = self;
    
    //注册:
    [self.languageTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"languageCell"];
    
    [self.view addSubview:self.languageTableView];
    [self.languageTableView release];
    
    //初始化语言tableView上的基本控件:
    //英文:
    self.englishLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.languageLabel.frame.origin.x, 20, ScreenWidth / 3, 40)];
    self.englishLabel.text = @"English";
    self.englishLabel.font = [UIFont systemFontOfSize:15];
    self.englishLabel.textColor = [UIColor blackColor];
    [self.languageTableView addSubview:self.englishLabel];
    [self.englishLabel release];
    self.langEngButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.langEngButton.frame = CGRectMake(ScreenWidth - 80, self.englishLabel.frame.origin.y - 10, 64, 64);
    
    [self.langEngButton setImage:[UIImage imageNamed:@"languageButtonG-1"] forState:UIControlStateNormal];
    
    [self.langEngButton addTarget:self action:@selector(didClickedEngButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.languageTableView addSubview:self.langEngButton];
    
    //中文:
    self.chineseLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.englishLabel.frame.origin.x, self.englishLabel.frame.origin.y + (ScreenHeight - 64 - 49) / 7, ScreenWidth / 3, 40)];
    self.chineseLabel.text = @"中文";
    self.chineseLabel.font = [UIFont systemFontOfSize:15];
    self.chineseLabel.textColor = [UIColor blackColor];
    [self.languageTableView addSubview:self.chineseLabel];
    [self.chineseLabel release];
    self.langChiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.langChiButton.frame = CGRectMake(ScreenWidth - 80, self.chineseLabel.frame.origin.y - 10, 64, 64);
    [self.langChiButton setImage:[UIImage imageNamed:@"languageButtonY-1"] forState:UIControlStateNormal];
    [self.langChiButton addTarget:self action:@selector(didClickedChiButton) forControlEvents:UIControlEventTouchUpInside];
    [self.languageTableView addSubview:self.langChiButton];

    self.a = 0;
    //设置隐藏:
    self.languageTableView.hidden = YES;
    
    //夜间模式:
    [self.tableView ea_setThemeContents:^(UIView *currentView, NSString *currentThemeIdentifier) {
       
        if ([currentThemeIdentifier isEqualToString:EAThemeBlack]) {
            self.tableView.backgroundColor = [UIColor grayColor];
            
        }
        
        else
        {
            self.tableView.backgroundColor = [UIColor whiteColor];
        }

    }];
    
}

#pragma mark ----- 必须实现的代理方法:(tableView)(languageTableView)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
         return 5;
    }
    if (tableView == self.languageTableView) {
        return 2;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        
        //夜间模式:
        [cell ea_setThemeContents:^(UIView *currentView, NSString *currentThemeIdentifier) {
            
            if ([currentThemeIdentifier isEqualToString:EAThemeBlack]) {
                cell.backgroundColor = [UIColor colorWithRed:0.08 green:0.08 blue:0.08 alpha:1.00];
                
                self.collectLabel.textColor = [UIColor yellowColor];
                self.clearLabel.textColor = [UIColor yellowColor];
                self.nightLabel.textColor = [UIColor yellowColor];
                self.languageLabel.textColor = [UIColor yellowColor];
                
                [self.s setOn:YES];
            }
            
            else
            {
                cell.backgroundColor = [UIColor whiteColor];
                
                self.collectLabel.textColor = [UIColor blueColor];
                self.clearLabel.textColor = [UIColor blueColor];
                self.nightLabel.textColor = [UIColor blueColor];
                self.languageLabel.textColor = [UIColor blueColor];

                [self.s setOn:NO];
            }
            
        }];

        
        return cell;

    }
    if (tableView == self.languageTableView) {
        UITableViewCell *cell = [self.languageTableView dequeueReusableCellWithIdentifier:@"languageCell"];
        //夜间模式:
        [cell ea_setThemeContents:^(UIView *currentView, NSString *currentThemeIdentifier) {
            
            if ([currentThemeIdentifier isEqualToString:EAThemeBlack]) {
                cell.backgroundColor = [UIColor grayColor];
                
            }
            
            else
            {
                cell.backgroundColor = [UIColor whiteColor];
            }
            
        }];
        

        
        
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (ScreenHeight - 64 - 49) / 7;
}


#pragma mark ---- 分享按钮点击方法:
- (void)didClickedShareButton
{
    MLWMapViewController *mvc = [[MLWMapViewController alloc] init];
    [self.navigationController pushViewController:mvc animated:YES];
    [mvc release];
}

#pragma mark ----- 我的收藏按钮:
- (void)didClickedCollectButton
{
    MLWCollectViewController *cvc = [[MLWCollectViewController alloc] init];
    [self.navigationController pushViewController:cvc animated:YES];
    [cvc release];
}


//计算图片缓存的值:
- (void)imageCacheValue:(NSInteger)accuracy      //精确到几位小数
{
    self.caCheNumber = ([[SDImageCache sharedImageCache] getSize] / 1024.00f) / 1024.00f;
    
}

//清除缓存的方法:
- (void)clearCache
{
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
}
#pragma mark ---- 清理缓存按钮:
- (void)didClickedClearButton
{
    CGFloat num = 0;
    //保留两位小数
    [self imageCacheValue:2];
    
    num = self.caCheNumber;
    
    NSString *str = [NSString stringWithFormat:@"当前缓存为 %.2f M", num];
    
    //创建警告框:
    UIAlertController *alearController = [UIAlertController alertControllerWithTitle:@"缓存管理" message:str preferredStyle:UIAlertControllerStyleAlert];
    //确认清除:
    UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确认清理" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //调用清理的方法:
        [self clearCache];
        [self imageCacheValue:2];
        
        NSLog(@"缓存已清空");
    }];
    //取消清除:
    
    UIAlertAction *actionCancle = [UIAlertAction actionWithTitle:@"取消清理" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"取消清理");
    }];
    
    //添加:
    [alearController addAction:actionConfirm];
    [alearController addAction:actionCancle];
    
    //显示:
    [self showDetailViewController:alearController sender:nil];
    
    
}
#pragma mark ----- 语言种类按钮:
- (void)didClickedLanguageButton
{
    //显示:
    self.a = self.a + 1;
    if (self.a % 2 == 1) {
         self.languageTableView.hidden = NO;
    }
    else
    {
        self.languageTableView.hidden = YES;
    }
    
    
}
#pragma mark ----- 英语按钮点击方法:
- (void)didClickedEngButton
{
   
    [self.langEngButton setImage:[UIImage imageNamed:@"languageButtonY-1"] forState:UIControlStateNormal];
    [self.langChiButton setImage:[UIImage imageNamed:@"languageButtonG-1"] forState:UIControlStateNormal];

}
#pragma mark ----- 中文点击方法:
- (void)didClickedChiButton
{
    [self.langEngButton setImage:[UIImage imageNamed:@"languageButtonG-1"] forState:UIControlStateNormal];
    [self.langChiButton setImage:[UIImage imageNamed:@"languageButtonY-1"] forState:UIControlStateNormal];

}

#pragma mark -- 开关触发方法:
- (void)didClickSwitch:(UISwitch *)sender
{
    if (sender.on == YES) {
       
        
        [[EAThemeManager shareManager] displayThemeContentsWithThemeIdentifier:EAThemeBlack];
    }
    else
    {
        
        [[EAThemeManager shareManager] displayThemeContentsWithThemeIdentifier:EAThemeNormal];
    }
}




   
        

   


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
